package io.chenglicun.modules.clc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.chenglicun.common.service.impl.CrudServiceImpl;
import io.chenglicun.common.constant.Constant;
import io.chenglicun.common.utils.ConvertUtils;
import io.chenglicun.modules.clc.dao.ClcExamResultDao;
import io.chenglicun.modules.clc.dto.*;
import io.chenglicun.modules.clc.entity.ClcExamResultEntity;
import io.chenglicun.modules.clc.service.*;
import io.chenglicun.modules.security.user.SecurityUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 考试结果
 *
 * @author HB 137664951@qq.com
 * @since 1.0 2023-10-18
 */
@Service
public class ClcExamResultServiceImpl extends CrudServiceImpl<ClcExamResultDao, ClcExamResultEntity, ClcExamResultDTO> implements ClcExamResultService {
    @Autowired
    private ClcQuestionService questionService;
    @Autowired
    private ClcExamReleaseService examReleaseService;
    @Autowired
    private ClcExamResultAnswerService examResultAnswerService;
    @Autowired
    private ClcQuestionAnswerService questionAnswerService;
    @Autowired
    private ClcPartyMemberScoreService partyMemberScoreService;

    @Override
    public QueryWrapper<ClcExamResultEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<ClcExamResultEntity> wrapper = new QueryWrapper<>();


        return wrapper;
    }

    @Override
    public ClcExamResultDTO get(Long id) {
        ClcExamResultDTO dto = super.get(id);

        ClcExamReleaseDTO releaseDTO = examReleaseService.get(dto.getReleaseId());
        List<Long> questionIds = releaseDTO.getExamReleaseQuestionList()
                .stream().map(ClcExamReleaseQuestionDTO::getQuestionId).collect(Collectors.toList());

        List<ClcQuestionDTO> questionDTOList =questionService.getByIds(questionIds);
        dto.setQuestionList(questionDTOList);

        List<ClcExamResultAnswerDTO> list = examResultAnswerService.getListByExamResultId(dto.getId());
        for (ClcQuestionDTO questionDTO : questionDTOList) {
            List<ClcExamResultAnswerDTO> collect = list.stream().filter(t -> t.getQuestionId().equals(questionDTO.getId())).collect(Collectors.toList());
            questionDTO.setExamResultAnswerList(collect);
        }

        return dto;
    }

    @Override
    public List<ClcExamResultDTO> getByUser() {
        Long userId = SecurityUser.getUserId();

        List<ClcExamResultEntity> entities = baseDao.selectList(
                new QueryWrapper<ClcExamResultEntity>()
                        .lambda().eq(ClcExamResultEntity::getUserId, userId)
        );
        return ConvertUtils.sourceToTarget(entities, ClcExamResultDTO.class);
    }

    @Override
    public List<ClcExamResultDTO> getByReleaseId(Long releaseId) {
        List<ClcExamResultEntity> entities = baseDao.selectList(
                new QueryWrapper<ClcExamResultEntity>()
                        .lambda().eq(ClcExamResultEntity::getReleaseId, releaseId)
        );
        return ConvertUtils.sourceToTarget(entities, ClcExamResultDTO.class);
    }

    @Override
    public ClcExamResultDTO getByUserAndReleaseId(Long releaseId) {
        Long userId = SecurityUser.getUserId();

        ClcExamResultEntity entity = baseDao.selectOne(
                new QueryWrapper<ClcExamResultEntity>()
                        .lambda().eq(ClcExamResultEntity::getUserId, userId)
                        .eq(ClcExamResultEntity::getReleaseId, releaseId)
                        .last("limit 1")
        );
        return this.get(entity.getId());
    }

    @Override
    @Transactional
    public void save(ClcExamResultDTO dto) {
        ClcExamReleaseDTO releaseDTO = examReleaseService.get(dto.getReleaseId());
        // 分数线
        Integer scoreLine = releaseDTO.getScoreLine();

        List<ClcExamResultAnswerDTO> list = dto.getExamResultAnswerList();
        Long[] examAnswerIds = list.stream().map(ClcExamResultAnswerDTO::getExamAnswerId).toArray(Long[]::new);
        List<ClcQuestionAnswerDTO> questionAnswerDTOList = questionAnswerService.getByIds(examAnswerIds);
        // 考试得分
        int score = questionAnswerDTOList.stream().mapToInt(ClcQuestionAnswerDTO::getScore).sum();

        dto.setScore(score);
        super.save(dto);

        list.forEach(t -> t.setExamResultId(dto.getId()));
        examResultAnswerService.saveBatch(list);

        if (score >= scoreLine) {
            Long userId = dto.getUserId();
            // 党员加分分数
            Integer scoreParty = releaseDTO.getScoreParty();
            // 增加党员分数
            partyMemberScoreService.addScore(userId, scoreParty);
        }
    }
}